#include <SFML/Graphics/View.hpp>
#include <nice2d/application.h>
#include <nice2d/scene.h>
#include <nice2d/widgets/minimap.h>
#include <nice2d/components/scenecamera.h>

IMPLEMENT_OBJECT(MiniMap,Control)

class MiniMapData
{
public:
    std::weak_ptr<Scene> scene;
    sf::View view;
};

MiniMap::MiniMap():
    data(new MiniMapData)
{
    data->view.reset(sf::FloatRect(0, 0, 2400, 1720));
}

MiniMap::~MiniMap()
{
}

void MiniMap::setScene(ScenePointer scene)
{
    data->scene = scene;
}

void MiniMap::setArea(const sf::FloatRect &area)
{
    data->view.reset(area);
}

void MiniMap::setViewport(const sf::FloatRect &viewport)
{
    data->view.setViewport(viewport);
}

sf::View MiniMap::getView() const
{
    return data->view;
}

void MiniMap::onDrawControl(sf::RenderTarget &target, sf::RenderStates states) const
{
    if (data->scene.expired())
        return;

    auto app = Application::getInstance();
    auto window = app->getWindow();

    auto scene = data->scene.lock();

    window->setView(data->view);
    target.draw(*data->scene.lock(),states);

    auto camera = GET_COMPONENT(scene,SceneCamera);
    if (camera) {
        auto view = camera->getView();
        window->setView(view);
    }
}
